import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.security.AccessControlException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class WhiteBoardServer extends UnicastRemoteObject implements IWhiteBoardServer {

	WhiteBoardModel model;

	HashMap<String, IWhiteBoardClient> clientsName = new HashMap<String, IWhiteBoardClient>();

	HashMap<String, Color> clientsColor = new HashMap<String, Color>();

	int colorCounter = 0;

	WhiteBoard board;

	ServerApplication slave;


	public WhiteBoardServer() throws RemoteException {
		super();

		try {

			try {
				java.rmi.registry.LocateRegistry.createRegistry(1099);
			} catch (Exception e) {
				e.printStackTrace();
			}
			Naming.rebind("localhost/Server", this);

		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		model = new WhiteBoardModel();
		board = new WhiteBoard(model, "White Board Server");
//		JFrame f = new JFrame("server");
//		f.setAlwaysOnTop(true);
//		f.add(board);
//		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		f.setSize(400, 400);
//		f.setVisible(true);

	}

	public WhiteBoard getBoard() {
		return board;
	}

	@Override
	public void login(String name, IWhiteBoardClient client, Color color) throws RemoteException {
		slave.appendMessage("Client " + name + " connecting.");
		clientsName.put(name, client);
		if (color == null || clientsColor.containsValue(color)) {
			clientsColor.put(
					name,
					new Color(Math.min((float) (Math.random() + 0.1), 1), Math.min(
							(float) (Math.random() + 0.1), 1), Math.min(
							(float) (Math.random() + 0.1), 1)));
			slave.appendMessage("A new color has been assigned to " + name );
		} else {
			slave.appendMessage("Proposed color accepted from " + name);
			clientsColor.put(name, color);
		}

	}
	
	

	@Override
	public void logout(String name) throws RemoteException {
		slave.appendMessage("Client " + name + " disconnecting...");
		if(clientsName.containsKey(name) && clientsColor.containsKey(name)){
			clientsName.remove(name);
			clientsColor.remove(name);
			model.removeLines(clientsColor.get(name));
			
		} 	else{
			slave.appendMessage("Error! client " + name + " not registred!");
		}
		

	}

	@Override
	public void line(String name, Point start, Point end) throws RemoteException {
		if(clientsColor.containsKey(name) && clientsName.containsKey(name)){
			model.addLine(start, end, clientsColor.get(name));
			board.repaint();
			clientsName.get(name).receiveLine(start, end, clientsColor.get(name));
		}
		
		// clientsName.get(name).receiveLine(start, end,
		// clientsColor.get(name));
		// TODO
	}

	public void setSlave(ServerApplication sa) {
		slave = sa;
		
	}

}
